package com.epoint.mobileoa.datanalysis;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.epoint.mobileoa.model.ArchiveList;
import com.epoint.mobileoa.model.ArchiveProcess;
import com.epoint.mobileoa.model.Attach;
import com.epoint.mobileoa.model.FrameOpinion;
import com.epoint.mobileoa.model.MailList;
import com.epoint.mobileoa.model.MailSendFromServiceModel;
import com.epoint.mobileoa.model.NoticeList;
import com.epoint.mobileoa.model.OperationBtn;
import com.epoint.mobileoa.model.Ou;
import com.epoint.mobileoa.model.User;
import com.epoint.mobileoa.model.WorkFlowProcessModel;
import com.epoint.mobileoa.ui.wsgw.model.FlowInfoModel;

/**
 * @author liyc
 * @time 2012-1-9 上午10:53:59
 * @annotation
 */
public class XMLHelp {
	public static final String headtag = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

	// 用户解析
	public static ArrayList<User> XML2UserList(String xml)
			throws DocumentException {
		Document document = null;
		document = DocumentHelper.parseText(headtag + xml);
		ArrayList list = new ArrayList();
		Element root = document.getRootElement();// 得到根元素
		List focs = root.elements();

		for (int i = 0; i < focs.size(); i++) {
			User user = new User();
			Element foc = (Element) focs.get(i);
			if (foc.element("ug") != null)
				user.userguid = foc.elementText("ug");
			if (foc.element("loginid") != null)
				user.loginid = foc.elementText("loginid");
			if (foc.element("og") != null)
				user.ouguid = foc.elementText("og");
			if (foc.element("dn") != null)
				user.displayname = foc.elementText("dn");
			if (foc.element("title") != null)
				user.title = foc.elementText("title");
			if (foc.element("telephoneoffice") != null)
				user.telephoneoffice = foc.elementText("telephoneoffice");
			if (foc.element("mobile") != null)
				user.mobile = foc.elementText("mobile");
			if (foc.element("email") != null)
				user.email = foc.elementText("email");
			if (foc.element("description") != null)
				user.description = foc.elementText("description");
			if (foc.element("telephonehome") != null)
				user.telephonehome = foc.elementText("telephonehome");
			if (foc.element("fax") != null)
				user.fax = foc.elementText("fax");
			if (foc.element("sex") != null)
				user.sex = foc.elementText("sex");
			if (foc.element("qqnumber") != null)
				user.qqnumber = foc.elementText("qqnumber");

			if (foc.element("ntxextnumber") != null)
				user.ntxextnumber = foc.elementText("ntxextnumber");

			if (foc.element("py") != null) {
				user.pyshou = foc.elementText("py");
			}
			list.add(user);
		}
		return list;
	}

	// 部门
	public static ArrayList<Ou> XML2OuList(String xml) throws DocumentException {

		Document document = null;
		document = DocumentHelper.parseText(headtag + xml);
		ArrayList list = new ArrayList();
		Element root = document.getRootElement();// 得到根元素
		List focs = root.elements();
		for (int i = 0; i < focs.size(); i++) {
			Ou ou = new Ou();
			Element foc = (Element) focs.get(i);
			if (foc.element("og") != null)
				ou.ouguid = foc.elementText("og");
			if (foc.element("on") != null)
				ou.ouname = foc.elementText("on");
			if (foc.element("pg") != null)
				ou.parentouguid = foc.elementText("pg");
			list.add(ou);
		}
		return list;
	}

	/**
	 * 解析邮件列表 liyc
	 * 
	 * @throws DocumentException
	 */
	public static List<MailList> AnalysisEmailListXML(String xml)
			throws DocumentException {
		Document document = null;
		document = DocumentHelper.parseText(headtag + xml);
		ArrayList mailList = new ArrayList();
		Element root = document.getRootElement();// 得到根元素
		List focs = root.elements();
		for (int i = 0; i < focs.size(); i++) {
			MailList emaillist = new MailList();
			Element foc = (Element) focs.get(i);
			if (foc.element("guid") != null)
				emaillist.guid = foc.elementText("guid");
			if (foc.element("title") != null)
				emaillist.title = foc.elementText("title");
			if (foc.element("msgFrom") != null)
				emaillist.msgFrom = foc.elementText("msgFrom");
			if (foc.element("time") != null)
				emaillist.time = foc.elementText("time");
			if (foc.element("status") != null)
				emaillist.status = foc.elementText("status");
			if (foc.element("attachment") != null)
				emaillist.attachment = foc.elementText("attachment");
			mailList.add(emaillist);
		}
		return mailList;
	}

	/**
	 * 解析邮件列表 liyc
	 * 
	 * @throws DocumentException
	 */
	public static List<MailSendFromServiceModel> AnalysisSendEmailListXML(
			String xml) {
		Document document = null;
		try {
			document = DocumentHelper.parseText(headtag + xml);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		ArrayList<MailSendFromServiceModel> list = new ArrayList<MailSendFromServiceModel>();
		Element root = document.getRootElement();// 得到根元素
		List focs = root.elements();
		for (int i = 0; i < focs.size(); i++) {
			MailSendFromServiceModel msfs = new MailSendFromServiceModel();
			Element foc = (Element) focs.get(i);
			if (foc.element("guid") != null)
				msfs.guid = foc.elementText("guid");
			if (foc.element("title") != null)
				msfs.title = foc.elementText("title");
			if (foc.element("msgto") != null)
				msfs.msgto = foc.elementText("msgto");
			if (foc.element("time") != null)
				msfs.time = foc.elementText("time");
			if (foc.element("status") != null)
				msfs.status = foc.elementText("status");
			if (foc.element("hasattach") != null)
				msfs.hasattach = foc.elementText("hasattach");
			if (foc.element("outmailguid") != null)
				msfs.outmailguid = foc.elementText("outmailguid");
			list.add(msfs);
		}
		return list;
	}

	/**
	 * 解析邮件列表 liyc
	 * 
	 * @throws DocumentException
	 */
	public static List<Attach> AttachListAna(String xml) {
		try {
			Document document = null;
			document = DocumentHelper.parseText(headtag + xml);
			ArrayList list = new ArrayList();
			Element root = document.getRootElement();// 得到根元素
			List focs = root.elements();
			for (int i = 0; i < focs.size(); i++) {
				Attach attach = new Attach();
				Element foc = (Element) focs.get(i);
				if (foc.element("attachfilename") != null)
					attach.attachfilename = foc.elementText("attachfilename");
				if (foc.element("url") != null)
					attach.url = foc.elementText("url");
				list.add(attach);
			}
			return list;
		} catch (DocumentException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 解析公告列表 liyc
	 * 
	 * @throws DocumentException
	 */
	public static List<NoticeList> NoticeListAna(String xml) {
		Document document = null;
		try {
			document = DocumentHelper.parseText(headtag + xml);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		ArrayList list = new ArrayList();
		Element root = document.getRootElement();// 得到根元素
		List focs = root.elements();
		for (int i = 0; i < focs.size(); i++) {
			NoticeList nl = new NoticeList();
			Element foc = (Element) focs.get(i);
			if (foc.element("guid") != null)
				nl.guid = foc.elementText("guid");
			if (foc.element("title") != null)
				nl.title = foc.elementText("title");
			if (foc.element("sender") != null)
				nl.sender = foc.elementText("sender");
			if (foc.element("time") != null)
				nl.time = foc.elementText("time");
			if (foc.element("hasattach") != null)
				nl.hasattach = foc.elementText("hasattach");
			list.add(nl);
		}
		return list;
	}

	/**
	 * 解析公文列表 liyc
	 * 
	 * @throws DocumentException
	 */
	public static List<ArchiveList> ArchiveListAna(String xml) {
		Document document = null;
		try {
			document = DocumentHelper.parseText(headtag + xml);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		ArrayList list = new ArrayList();
		Element root = document.getRootElement();// 得到根元素
		List focs = root.elements();
		for (int i = 0; i < focs.size(); i++) {
			ArchiveList al = new ArchiveList();
			Element foc = (Element) focs.get(i);
			if (foc.element("title") != null)
				al.title = foc.elementText("title");
			if (foc.element("messageitemguid") != null)
				al.messageitemguid = foc.elementText("messageitemguid");
			if (foc.element("beizhu") != null)
				al.beizhu = foc.elementText("beizhu");
			if (foc.element("generatedate") != null)
				al.generatedate = foc.elementText("generatedate");
			if (foc.element("tijiaoren") != null)
				al.tijiaoren = foc.elementText("tijiaoren");
			if (foc.element("banliren") != null)
				al.banliren = foc.elementText("banliren");
			if (foc.element("archiveguid") != null)
				al.archiveguid = foc.elementText("archiveguid");
			if (foc.element("archivetype") != null)
				al.archivetype = foc.elementText("archivetype");
			list.add(al);
		}
		return list;
	}

	/**
	 * 解析公文处理按钮liyc
	 * 
	 * @throws DocumentException
	 */
	public static List<OperationBtn> OperationBtnAna(String xml) {
		Document document = null;
		try {
			document = DocumentHelper.parseText(headtag + xml);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		ArrayList list = new ArrayList();
		Element root = document.getRootElement();// 得到根元素
		List focs = root.elements();
		for (int i = 0; i < focs.size(); i++) {
			OperationBtn ob = new OperationBtn();
			Element foc = (Element) focs.get(i);
			if (foc.element("operationGuid") != null)
				ob.operationGuid = foc.elementText("operationGuid");
			if (foc.element("operationname") != null)
				ob.operationname = foc.elementText("operationname");
			if (foc.element("nextActivityGuid") != null)
				ob.nextActivityGuid = foc.elementText("nextActivityGuid");
			if (foc.element("nextActivityName") != null)
				ob.nextActivityName = foc.elementText("nextActivityName");
			if (foc.element("transitionGuid") != null)
				ob.transitionGuid = foc.elementText("transitionGuid");
			if (foc.element("transactors") != null)
				ob.transactors = foc.elementText("transactors");
			if (foc.element("operateStatus") != null)
				ob.operateStatus = foc.elementText("operateStatus");
			if (foc.element("ShowOperationPage") != null)
				ob.ShowOperationPage = foc.elementText("ShowOperationPage");
			if (foc.element("ShowNextActivit") != null)
				ob.ShowNextActivit = foc.elementText("ShowNextActivit");
			list.add(ob);
		}
		return list;
	}

	/**
	 * 解析公文处理过程liyc
	 * 
	 * @throws DocumentException
	 */
	public static List<ArchiveProcess> ArchiveProcessAna(String xml) {
		Document document = null;
		try {
			document = DocumentHelper.parseText(headtag + xml);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		ArrayList list = new ArrayList();
		Element root = document.getRootElement();// 得到根元素
		List focs = root.elements();
		for (int i = 0; i < focs.size(); i++) {
			ArchiveProcess ap = new ArchiveProcess();
			Element foc = (Element) focs.get(i);
			if (foc.element("step") != null)
				ap.step = foc.elementText("step");
			if (foc.element("dealer") != null)
				ap.dealer = foc.elementText("dealer");
			if (foc.element("receiveDate") != null)
				ap.receiveDate = foc.elementText("receiveDate");
			if (foc.element("readDate") != null)
				ap.readDate = foc.elementText("readDate");
			if (foc.element("opinion") != null)
				ap.opinion = foc.elementText("opinion");
			if (foc.element("senderName") != null)
				ap.senderName = foc.elementText("senderName");
			if (foc.element("dealDate") != null)
				ap.dealDate = foc.elementText("dealDate");
			list.add(ap);
		}
		return list;
	}

	public static Element getRoot(String xml) {
		Document document = null;
		try {
			document = DocumentHelper.parseText(XMLHelp.headtag + xml);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		Element root = document.getRootElement();// 得到根元素
		return root;
	}

	public static List<WorkFlowProcessModel> WorkFlowProcessAnalysis(String xml) {

		List focs = XMLHelp.getRoot(xml).elements();
		List<WorkFlowProcessModel> plist = new ArrayList<WorkFlowProcessModel>();
		for (int i = 0; i < focs.size(); i++) {
			WorkFlowProcessModel mt = new WorkFlowProcessModel();
			Element foc = (Element) focs.get(i);
			if (foc.element("step") != null)
				mt.step = foc.elementText("step");
			if (foc.element("dealer") != null)
				mt.dealer = foc.elementText("dealer");
			if (foc.element("shoudaoTime") != null)
				mt.shoudaoTime = foc.elementText("shoudaoTime");
			if (foc.element("readDate") != null)
				mt.readDate = foc.elementText("readDate");
			if (foc.element("dealDate") != null)
				mt.dealDate = foc.elementText("dealDate");
			if (foc.element("opinion") != null)
				mt.opinion = foc.elementText("opinion");
			if (foc.element("senderName") != null)
				mt.senderName = foc.elementText("senderName");
			if (foc.element("OperationType") != null)
				mt.OperationType = foc.elementText("OperationType");
			if (foc.element("OperationName") != null)
				mt.OperationName = foc.elementText("OperationName");
			plist.add(mt);
		}
		return plist;
	}

	/**
	 * 
	 * @author lilin
	 * @date 2012-5-10 下午5:01:37
	 * @annotation 解析意见XML
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<FrameOpinion> JieXiOpinion(String xml)
			throws DocumentException {
		Document document = null;
		document = DocumentHelper.parseText(headtag + xml);
		ArrayList list = new ArrayList();
		Element root = document.getRootElement();// 得到根元素
		List focs = root.elements();
		for (int i = 0; i < focs.size(); i++) {
			FrameOpinion model = new FrameOpinion();
			Element foc = (Element) focs.get(i);
			if (foc.element("text") != null)
				model.opinionText = foc.elementText("text");
			if (foc.element("order") != null)
				model.orderNumber = foc.elementText("order");
			list.add(model);
		}
		return list;
	}

	/**
	 * 
	 * @author lilin
	 * @date 2012-8-8 上午11:09:31
	 * @annotation 拼接处理过程的XML
	 */
	public static String pingProcessXML(List<ArchiveProcess> alist) {
		String logHtml = "";
		String center = "<div align=\"center\">";
		String fontcolor = "black";
		String titlecolor = "#9A32CD";
		logHtml += "<table width='100%'  border=\"0\" cellspacing=\"1\" style=\"background-color:#a0c6e5\">";
		logHtml += "<tr bgcolor=\"#CCCCCC\">";
		logHtml += "<td>" + center + "<font color=" + titlecolor
				+ " size=1>步骤</font></td>";
		logHtml += "<td>" + center + "<font color=" + titlecolor
				+ " size=1>办理人员</font></td>";
		logHtml += "<td>" + center + "<font color=" + titlecolor
				+ " size=1>收到时间</font></td>";
		logHtml += "<td>" + center + "<font color=" + titlecolor
				+ " size=1>阅读时间</font></td>";
		logHtml += "<td>" + center + "<font color=" + titlecolor
				+ " size=1>处理时间</font></td>";
		logHtml += "<td>" + center + "<font color=" + titlecolor
				+ " size=1>办理意见</font></td>";
		logHtml += "<td>" + center + "<font color=" + titlecolor
				+ " size=1>提交人</font></td>";
		logHtml += "</tr>";
		for (int i = 0; i < alist.size(); i++) {
			String bgcolor = "#F0F0F0";
			if (i % 2 == 0) {
				bgcolor = "#FFF0F5";// 淡红
			}

			logHtml += "<tr bgcolor=" + bgcolor + ">";
			logHtml += "<td  >" + center + "<font color=" + fontcolor
					+ " size=2>" + alist.get(i).step + "</font></td>";// 步骤
			logHtml += "<td>" + center + "<font color=" + fontcolor
					+ " size=1>" + alist.get(i).dealer + "</font></td>";// 办理人员
			logHtml += "<td>" + center + "<font color=" + fontcolor
					+ " size=1>" + alist.get(i).receiveDate + "</font></td>";// 收到时间

			logHtml += "<td>" + center + "<font color=" + fontcolor
					+ " size=1>" + alist.get(i).readDate + "</font></td>";// 阅读时间
			logHtml += "<td>" + center + "<font color=" + fontcolor
					+ " size=1>" + alist.get(i).dealDate + "</font></td>";// 处理时间
			logHtml += "<td>" + center + "<font color=" + fontcolor
					+ " size=1>" + alist.get(i).opinion + "</font></td>";// 办理意见
			logHtml += "<td>" + center + "<font color=" + fontcolor
					+ " size=1>" + alist.get(i).senderName + "</font></td>";// 提交人
			logHtml += "</tr>";
		}
		logHtml += "</table>";
		logHtml += "<br>";
		logHtml += "<br>";
		return logHtml;
	}
	
	/**
	 * 
	 * @author lilin
	 * @date 2012-8-8 上午11:09:31
	 * @annotation 拼接处理过程的XML
	 */
	public static String pingProcessXML_WS(List<FlowInfoModel> alist) {
		String logHtml = "";
		String center = "<div align=\"center\">";
		String fontcolor = "black";
		String titlecolor = "#9A32CD";
		logHtml += "<table width='100%'  border=\"0\" cellspacing=\"1\" style=\"background-color:#a0c6e5\">";
		logHtml += "<tr bgcolor=\"#CCCCCC\">";
		logHtml += "<td>" + center + "<font color=" + titlecolor
				+ " size=1>步骤</font></td>";
		logHtml += "<td>" + center + "<font color=" + titlecolor
				+ " size=1>办理人员</font></td>";
		logHtml += "<td>" + center + "<font color=" + titlecolor
				+ " size=1>收到时间</font></td>";
//		logHtml += "<td>" + center + "<font color=" + titlecolor
//				+ " size=1>阅读时间</font></td>";
		logHtml += "<td>" + center + "<font color=" + titlecolor
				+ " size=1>处理时间</font></td>";
		logHtml += "<td>" + center + "<font color=" + titlecolor
				+ " size=1>办理意见</font></td>";
//		logHtml += "<td>" + center + "<font color=" + titlecolor
//				+ " size=1>提交人</font></td>";
		logHtml += "</tr>";
		for (int i = 0; i < alist.size(); i++) {
			String bgcolor = "#F0F0F0";
			if (i % 2 == 0) {
				bgcolor = "#FFF0F5";// 淡红
			}

			logHtml += "<tr bgcolor=" + bgcolor + ">";
			logHtml += "<td  >" + center + "<font color=" + fontcolor
					+ " size=2>" + alist.get(i).ActivityName + "</font></td>";// 步骤
			logHtml += "<td>" + center + "<font color=" + fontcolor
					+ " size=1>" + alist.get(i).HandleUserName + "</font></td>";// 办理人员
			logHtml += "<td>" + center + "<font color=" + fontcolor
					+ " size=1>" + alist.get(i).ReceiveDateTime + "</font></td>";// 收到时间

//			logHtml += "<td>" + center + "<font color=" + fontcolor
//					+ " size=1>" + alist.get(i).readDate + "</font></td>";// 阅读时间
			logHtml += "<td>" + center + "<font color=" + fontcolor
					+ " size=1>" + alist.get(i).HandleDateTime + "</font></td>";// 处理时间
			logHtml += "<td>" + center + "<font color=" + fontcolor
					+ " size=1>" + alist.get(i).Opinion + "</font></td>";// 办理意见
//			logHtml += "<td>" + center + "<font color=" + fontcolor
//					+ " size=1>" + alist.get(i).senderName + "</font></td>";// 提交人
			logHtml += "</tr>";
		}
		logHtml += "</table>";
		logHtml += "<br>";
		logHtml += "<br>";
		return logHtml;
	}
}
